package com.jt.project;

import com.jt.datasource.JedisDataSource;
import redis.clients.jedis.Jedis;

import java.util.Map;

public class CartDemo01 {
    public static void main(String[] args) {
        Long userId=1001L;

        Long goods1=201L;
        Long goods2=202L;
        Long goods3=203L;

        addCart(userId,goods1,1);
        addCart(userId,goods2,2);
        addCart(userId,goods3,3);

        Map map=listCart(userId);
        System.out.println(map);
    }

    private static Map listCart(Long userId) {
        Jedis jedis= JedisDataSource.getConnection();
        Map<String, String> userCart = jedis.hgetAll("cart:" + userId);
        jedis.close();
        return userCart;
    }

    private static void addCart(Long userId, Long goods, int count) {
        Jedis jedis= JedisDataSource.getConnection();
        jedis.hincrBy("cart:"+userId, String.valueOf(goods), count);
        jedis.close();
    }


}
